package org.zjx.state;

import org.zjx.common.bean.Role;
import org.zjx.db.RoleDB;

import java.util.HashMap;
import java.util.Map;

public class StateManager {
    public Map<String, PlayerState> stateMap = new HashMap<>();

    private static final class InstanceHolder {
        static final StateManager instance = new StateManager();
    }

    public static StateManager getInstance() {
        return InstanceHolder.instance;
    }

    private StateManager(){

    }

    public PlayerState getPlayerState(String playerId) {
        if (stateMap.containsKey(playerId)) {
            return stateMap.get(playerId);
        }

        Role role = new RoleDB().findByRoleId(playerId);

        if (role == null) return null;

        PlayerState playerState = new PlayerState();
        playerState.setRoleId(role.getId());

        stateMap.put(playerId, playerState);
        return playerState;
    }

}
